#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define TYPE int

typedef struct Stack
{
	TYPE* arr;	// 内存首地址
	int len;	// 栈的容量
	int top;	// 栈顶下标
}Stack;

// 创建
Stack* creat_stack(size_t cal)
{
	Stack* stack = malloc(sizeof(Stack));
	stack->arr = malloc(sizeof(TYPE)*cal);
	stack->len = cal;
	stack->top = -1;
	return stack;
}

// 销毁
void destory_stack(Stack* stack)
{
	free(stack->arr);
	free(stack);
}

// 栈空
bool empty_stack(Stack* stack)
{
	return -1 == stack->top;
}

// 栈满
bool full_stack(Stack* stack)
{
	return stack->top+1 >= stack->len;
}

// 入栈
bool push_stack(Stack* stack,TYPE data)
{
	if(full_stack(stack)) return false;

	stack->arr[++stack->top] = data;
	return true;
}

// 出栈
bool pop_stack(Stack* stack)
{
	if(empty_stack(stack)) return false;
	stack->top--;
	return true;
}

// 栈顶
TYPE* top_stack(Stack* stack)
{
	if(empty_stack(stack)) return NULL;
	return stack->arr + stack->top;
}

int main()
{
	Stack* stack = creat_stack(15);
	printf("开始入栈：\n");
	for(int i=0; i<20; i++)
	{
		printf("入栈%s，",push_stack(stack,i)?"成功":"失败");
		printf("栈顶：%d\n",*top_stack(stack));
	}
	printf("------------------------\n开始出栈：\n");
	for(int i=0; i<15; i++)
	{
		printf("栈顶：%2d，",*top_stack(stack));
		printf("出栈：%s\n",pop_stack(stack)?"成功":"失败");
	}
	
}



